<%@ Page language="vb" CodeFile="settingsAdditionalProduct.aspx.vb" Inherits="settingsAdditionalProduct_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        additionalProductID = Request.QueryString("additionalProductID")
        productID = Request.QueryString("productID")
%>
<script type="text/javascript">
  function confirmSubmit() { 
	if (window.confirm('Are you sure you want to delete the additional product?')) {
		location.href='settingsAdditionalProduct.aspx?do=delete&productID=<%= productID %>&additionalProductID=<%= additionalProductID %>';
		return false;
	}
	else {
		return false;
	}
  }
  </script>
<%
        If Len(productID) > 0 Then 
            productID = CLng(productID)
            dbOpen()
            RecSet = Conn.Execute("Select p.product, c.country, c.countryCurrency From (tblProduct p INNER JOIN tblCountry c ON c.countryID = p.countryID) Where p.productID = " & productID & "")
            If RecSet.EOF Then 
                productID = ""
            Else
                dbProduct = RecSet.Fields("product").Value
                dbCountry = RecSet.Fields("country").Value
                dbCountryCurrency = RecSet.Fields("countryCurrency").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        If Len(additionalProductID) > 0 Then 
            additionalProductID = CLng(additionalProductID)
            dbOpen()
            RecSet = Conn.Execute("Select ap.additionalProduct, ap.additionalProductCategory, ap.byDefault, ap.price, ap.uw, (Select Count(apvc.additionalProductID) From tblAdditionalProductVehicleContent apvc Where apvc.additionalProductID = ap.additionalProductID) As vehicleCount From tblAdditionalProduct ap Where ap.additionalProductID = " & additionalProductID & "")
            If RecSet.EOF Then 
                productID = ""
            Else
                dbAdditionalProduct = RecSet.Fields("additionalProduct").Value
                dbAdditionalProductCategory = RecSet.Fields("additionalProductCategory").Value
                dbVehicleCount = RecSet.Fields("vehicleCount").Value
                dbPrice = RecSet.Fields("price").Value
                dbUw = RecSet.Fields("uw").Value
                dbByDefault = RecSet.Fields("byDefault").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        If Len(dbByDefault) = 0 Then 
            dbByDefault = 0
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""additionalProduct"", ""==''""],"
        strJS = strJS + "[""additionalProductCategory"", ""==''""],"
        strJS = strJS + "[""price"", ""==''""],"
        strJS = strJS + "[""uw"", ""==''""],"
        If CInt(dbVehicleCount) = 0 Then 
            strJS = strJS + "[""byDefault"", ""<1""],"
        End IF
        strJS = Left(strJS, Len(strJS) - 1)
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>ADDITIONAL PRODUCT SETTINGS<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='settingsAdditionalProduct.aspx?do=save&productID=" & productID & "&additionalProductID=" & additionalProductID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Country:</td><td align='right'><input type='text' name='country' id='country' size='40' maxlength='50' value='" & dbCountry & "' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Head product:</td><td align='right'><input type='text' name='product' id='product' size='40' maxlength='50' value='" & dbProduct & "' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Additional product:</td><td align='right'><input type='text' name='additionalProduct' id='additionalProduct' size='40' maxlength='50' value='" & dbAdditionalProduct & "'></td><td><div id='error_additionalProduct' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Additional product Category:</td><td align='right'><input type='text' name='additionalProductCategory' id='additionalProductCategory' size='40' maxlength='50' value='" & dbAdditionalProductCategory & "'></td><td><div id='error_additionalProductCategory' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Price:</td><td align='right'><input type='text' name='price' id='price' size='40' maxlength='50' value='" & dbPrice & "'></td><td><div id='error_price' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>UW:</td><td align='right'><input type='text' name='uw' id='uw' size='40' maxlength='50' value='" & dbUw & "'></td><td><div id='error_uw' class='edit_error'>&laquo; OBS!</div></td></tr>")
        If CInt(dbVehicleCount) = 0 Then 
            Response.Write("<tr>")
            Response.Write("<td class='formBold'>Added by default:</td>")
            Response.Write("<td align='right'>")
            Response.Write("<select name='byDefault' id='byDefault'>")
            Response.Write("<option value='2'> No ")
            Response.Write("<option value='1'")
            If CInt(dbByDefault) = 1 Then 
                Response.Write(" selected='selected'")
            End IF
            Response.Write("> Yes ")
            Response.Write("<option value=''> -------------------------------------- ")
            Response.Write("</select>")
            Response.Write("</td>")
            Response.Write("<td><div id='error_byDefault' class='edit_error'>&laquo; OBS!</div></td>")
            Response.Write("</tr>")
        Else
            Response.Write("<tr><td class='formBold'>Added by default:</td><td align='right'><input type='text' name='strByDefault' id='strByDefault' size='40' maxlength='50' value='No' readonly='readonly'></td></tr>")
            Response.Write("<tr><td colspan='2'><font color='red'>There are manually connected lease contracts to this additional product.<br>Please edit the lease contracts before editing the ""Added by default"" value.</font></td></tr>")
            Response.Write("<input type='hidden' name='byDefault' id='byDefault' size='40' maxlength='50' value='0'>")
        End IF
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 And Len(additionalProductID) > 0 Then 
            Response.Write("<input type='button' value='Delete additional product' onClick='return confirmSubmit()'>&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='7'>EXISTING ADDITIONAL PRODUCTS FOR HEAD PRODUCT " & UCase(dbProduct) & "<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'>")
        Response.Write("<td class='formBold'>Additional product</td>")
        Response.Write("<td class='formBold'>Product code</td>")
        Response.Write("<td class='formBold'>Added by default</td>")
        Response.Write("<td class='formBold'>Price</td>")
        Response.Write("<td class='formBold'>Uw</td>")
        Response.Write("<td class='formBold' align='right'>Number of contracts</td>")
        Response.Write("<td class='formBold'></td>")
        Response.Write("</tr>")
        rowNumber = 1
        dbOpen()
        RecSet = Conn.Execute("Select ap.additionalProductID, ap.additionalProduct, ap.additionalProductCategory, ap.byDefault, ap.price, ap.uw, c.countryCurrency, (Select Count(vehicleID) From tblVehicle Where productID = ap.productID) As vehicleCount, (Select Count(vehicleID) From tblAdditionalProductVehicleContent Where additionalProductID = ap.additionalProductID) As additionalProductVehicleCount From ((tblAdditionalProduct ap INNER JOIN tblProduct p ON p.productID = ap.productID) INNER JOIN tblCountry c ON c.countryID = p.countryID) Where ap.productID = " & productID & " Order by ap.additionalProduct")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No additional products found.</font></td></tr>")
        Else
            Do Until RecSet.EOF
                additionalProductID = RecSet.Fields("additionalProductID").Value
                additionalProduct = RecSet.Fields("additionalProduct").Value
                additionalProductCategory = RecSet.Fields("additionalProductCategory").Value
                byDefault = RecSet.Fields("byDefault").Value
                price = RecSet.Fields("price").Value
                uw = RecSet.Fields("uw").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                vehicleCount = RecSet.Fields("vehicleCount").Value
                additionalProductVehicleCount = RecSet.Fields("additionalProductVehicleCount").Value
                If CInt(byDefault) = 0 Then 
                    byDefault = "<font color='red'>No</font>"
                    vehicleCount = additionalProductVehicleCount
                Else
                    byDefault = "<font color='green'>Yes</font>"
                    vehicleCount = vehicleCount
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & additionalProduct & "</td>")
                Response.Write("<td>" & additionalProductCategory & "</td>")
                Response.Write("<td>" & byDefault & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(price), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(uw), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(vehicleCount), 0) & "</td>")
                Response.Write("<td align='right'><a href='settingsAdditionalProduct.aspx?additionalProductID=" & additionalProductID & "&productID=" & productID & "'>edit</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='7'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        additionalProduct = Request.Form("additionalProduct")
        additionalProductCategory = Request.Form("additionalProductCategory")
        price = Request.Form("price")
        uw = Request.Form("uw")
        byDefault = Request.Form("byDefault")
        productID = Request.QueryString("productID")
        additionalProductID = Request.QueryString("additionalProductID")
        productID = CLng(productID)
        If Len(additionalProduct) > 0 Then 
            additionalProduct = Trim(additionalProduct)
            additionalProduct = Replace(CStr(additionalProduct), "'", "")
            additionalProduct = Replace(CStr(additionalProduct), """", "")
            additionalProduct = Replace(CStr(additionalProduct), "<", "")
            additionalProduct = Replace(CStr(additionalProduct), ">", "")
            additionalProduct = Replace(CStr(additionalProduct), "=", "")
        End IF
        If Len(additionalProductCategory) > 0 Then 
            additionalProductCategory = Trim(additionalProductCategory)
            additionalProductCategory = Replace(CStr(additionalProductCategory), "'", "")
            additionalProductCategory = Replace(CStr(additionalProductCategory), """", "")
            additionalProductCategory = Replace(CStr(additionalProductCategory), "<", "")
            additionalProductCategory = Replace(CStr(additionalProductCategory), ">", "")
            additionalProductCategory = Replace(CStr(additionalProductCategory), "=", "")
        End IF
        If Len(price) > 0 Then 
            price = Trim(price)
            price = Replace(CStr(price), ",", ".")
        Else
            price = 0
        End IF
        If Len(uw) > 0 Then 
            uw = Trim(uw)
            uw = Replace(CStr(uw), ",", ".")
        Else
            uw = 0
        End IF
        If Len(byDefault) > 0 Then 
            byDefault = CLng(byDefault)
            If CInt(byDefault) = 2 Then 
            byDefault = 0
        ElseIf CInt(byDefault) = 1 Then
            price = 0
            uw = 0
            End If
        End IF
        If Len(additionalProductID) > 0 Then 
            additionalProductID = CLng(additionalProductID)
            dbOpen()
            Response.Write("Update tblAdditionalProduct Set additionalProduct = '" & additionalProduct & "', additionalProductCategory = '" & additionalProductCategory & "', price = " & price & ", uw = " & uw & ", byDefault = " & byDefault & " Where additionalProductID = " & additionalProductID & "<br>")
            Conn.Execute("Update tblAdditionalProduct Set additionalProduct = '" & additionalProduct & "', additionalProductCategory = '" & additionalProductCategory & "', price = " & price & ", uw = " & uw & ", byDefault = " & byDefault & " Where additionalProductID = " & additionalProductID & "")
            dbClose()
        Else
            dbOpen()
            Response.Write("Insert Into tblAdditionalProduct (productID, additionalProduct, additionalProductCategory, price, uw, byDefault) Values(" & productID & ",'" & additionalProduct & "','" & additionalProductCategory & "'," & price & "," & uw & "," & byDefault & ")<br>")
            Conn.Execute("Insert Into tblAdditionalProduct (productID, additionalProduct, additionalProductCategory, price, uw, byDefault) Values(" & productID & ",'" & additionalProduct & "','" & additionalProductCategory & "'," & price & "," & uw & "," & byDefault & ")")
            dbClose()
        End IF
        Response.Redirect("settingsAdditionalProduct.aspx?subDo=%20-%20SAVED&productID=" & productID & "")
    ElseIf Request.QueryString("do") = "delete" Then 
        additionalProductID = Request.QueryString("additionalProductID")
        productID = Request.QueryString("productID")
        additionalProduct = CLng(additionalProduct)
        productID = CLng(productID)
        dbVehicleCount = 1
        dbOpen()
        RecSet = Conn.Execute("Select Count(additionalProductID) As vehicleCount From tblAdditionalProductVehicleContent Where additionalProductID = " & additionalProductID & "")
        dbVehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 Then 
            dbOpen()
            Conn.Execute("Delete From tbladditionalProduct Where additionalProductID = " & additionalProductID & "")
            dbClose()
        End IF
        Response.Redirect("settingsAdditionalProduct.aspx?subDo=%20-%20DELETED&productID=" & productID & "")
    End IF
%>
